import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
const routes = [
  {
    path: "/",
    component: () => import("./pages/Home.vue"),
    name: "Home",
  },
  {
    path: "/cart",
    component: () => import("./pages/Cart.vue"),
    name: "Cart",
    meta: {
      needLogin: true,
    },
  },
  {
    path: "/hot",
    component: () => import("./pages/Hot.vue"),
    name: "Hot",
  },
  {
    path: "/login",
    component: () => import("./pages/Login.vue"),
    name: "Login",
  },
  {
    path: "/reg",
    component: () => import("./pages/Reg.vue"),
    name: "Reg",
  },
  {
    path: "/user",
    component: () => import("./pages/User.vue"),
    name: "User",
    meta: {
      needLogin: true,
    },
  },
  {
    path: "/detail",
    component: () => import("./pages/Detail.vue"),
    name: "Detail",
  },
];
const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => {
  console.log(to);

  if (to.meta.needLogin) {
    if (sessionStorage.getItem("token")) {
      console.log(to);
      next();
    } else {
      next({
        name: "Login",
      });
    }
  } else {
    next();
  }
});
export default router;
